package com.revoltingdevelopment.perquackey;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.lang.*;
import java.io.*;
import java.util.*;

public class PerquackeyApp {

	public static void main(String[] args) {
            System.out.println("I'm trying invokeLater now");

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame frame = new PerquackeyFrame();
					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setVisible(true);
				}
				catch (FileNotFoundException e) {}
			}
		});

            System.out.println("how'd uyou like it?");
	}

}

class PerquackeyFrame extends JFrame {

	public PerquackeyFrame()
		throws FileNotFoundException {

		System.out.println("Loading dictionary...");
		try {
			dictionaryFile = new Scanner(new File("../data/prod/sowpods.txt"));
			while (dictionaryFile.hasNextLine()) {
				String theLine = dictionaryFile.nextLine();
				if (theLine.length() > 2 && theLine.length() < 11) {
					theDictionary.add(theLine);
				}
			}
		}
		finally {
			if (dictionaryFile != null) { dictionaryFile.close(); }
		}
		System.out.println("Dictionary loaded...starting GUI...");

		setLayout(new GridLayout(3, 1));
		setTitle("Perquackey");
		setSize(800, 600);
		final JTextField textEntry = new JTextField(20);

		// Letters from PerquackeyDiceCupAlt.
		final PerquackeyDiceCupAlt theCupAlt = new PerquackeyDiceCupAlt();
		theCupAlt.roll();
		PerquackeyDieAlt[] theDice = theCupAlt.getDice(true);
		final TableModel chartModel = new WordChartModel(chart);
		final JTable chartTable = new JTable(chartModel);
		chartTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		for (int j = 0; j <= 7; j++) {
			TableColumn col = chartTable.getColumnModel().getColumn(j);
			col.setMinWidth(50);
			col.setMaxWidth(50);
			col.setPreferredWidth(50);
		}
		final JLabel scoreboardLabel = new JLabel("*******Scoreboard*********");
		final JLabel timerLabel = new JLabel("" + seconds);
		final JButton endTurnButton = new JButton("End Turn");
		final JButton newGameButton = new JButton("New Game");
		final JButton endGameButton = new JButton("End Game");
		final JButton pauseGameButton = new JButton("Pause Game");
		final JLabel statusLabel = new JLabel("Status: ");
		final JLabel tedLabel = new JLabel(ted.getName() + " " + ted.getPoints());
		final JLabel billLabel = new JLabel(bill.getName() + " " + bill.getPoints());

		JPanel dicePanel = new JPanel(new GridLayout(2, 7));
		for (PerquackeyDieAlt theDie : theDice) {
			String theImageFile = "../images/PQ" + theDie.getLetter() + ".gif";
			dicePanel.add(new JButton(new ImageIcon(theImageFile)));
		}

		JPanel controlPanel = new JPanel(new GridLayout(3, 4));
		controlPanel.add(scoreboardLabel);
		controlPanel.add(timerLabel);
		controlPanel.add(textEntry);
		controlPanel.add(endTurnButton);
		controlPanel.add(newGameButton);
		controlPanel.add(endGameButton);
		controlPanel.add(pauseGameButton);
		controlPanel.add(statusLabel);
		controlPanel.add(tedLabel);
		controlPanel.add(billLabel);

		add(controlPanel);
		add(dicePanel); 
		add(chartTable);

		// Update the timer display every second till it reaches zero.
		// In actual game, timer will be reset after each throw of the dice
		// and will displa in minute:seconds format, or even nicer image style.
		final javax.swing.Timer theTimer = new javax.swing.Timer(delay,
			new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
				}
			});

		theTimer.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					--seconds;
					if (seconds == 0) {
						theTimer.stop();
						timerLabel.setText("Time's Up!");
					}
					else {
						timerLabel.setText("" + seconds);
					}
				}
			});

		theTimer.start();
		
		// Handle words keyed in textEntry field.
		textEntry.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int key = e.getKeyCode();
				if (key == KeyEvent.VK_ENTER) {
					String word = textEntry.getText().trim();
					if (chart.addWord(word, theCupAlt, 2000, theDictionary)) {
						chartModel.setValueAt(chart, 0, 0);
						textEntry.setText("");
					}
					statusLabel.setText(chart.getStatusMsg());
				}
			}
		});

		endTurnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int points = chart.getScore();
				if (tedPlayer) {
					ted.addPoints(points);
					tedLabel.setText(ted.getName() + " " + ted.getPoints());
					tedPlayer = false;
				}
				else {
					bill.addPoints(points);
					billLabel.setText(bill.getName() + " " + bill.getPoints());
					tedPlayer = true;
				}
				chart = new WordChart();
				chartModel.setValueAt(chart, 0, 0);
			}
		});
	}

	private int delay = 1000;
	private int seconds = 120;
	private WordChart chart = new WordChart();
	private ArrayList<Player> thePlayers = new ArrayList<Player>();
	private Player ted = new Player("Ted");
	private Player bill = new Player("Bill");
	private boolean tedPlayer = true;
	private Scanner dictionaryFile = null;
	private TreeSet<String> theDictionary = new TreeSet<String>();
}

class WordChartModel extends AbstractTableModel {

	public WordChartModel(WordChart chart) {

		wordTable = chart.getWordChart();
	}

	public void setValueAt(Object chart, int row, int col) {

		WordChart myChart = (WordChart)chart;
		wordTable = myChart.getWordChart();
		fireTableDataChanged();
		fireTableStructureChanged();
	}

	public int getRowCount() {

		return wordTable.length;
	}

	public int getColumnCount() {

		return wordTable[0].length;
	}

	public Object getValueAt(int row, int col) {

		return wordTable[row][col];
	}

	public String getColumnName(int col) {

		Integer colNum = new Integer(col + 3);
		return colNum.toString();
	}

	private Object[][] wordTable = new Object[5][8];
}
